package cn.wp.common.core.web;

import cn.wp.common.core.WpProperties;
import cn.wp.common.core.WpVar;
import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.exception.WpRestServiceException;
import cn.wp.common.core.module.log.AccessLogDto;
import cn.wp.common.core.module.log.AccessLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 全局的的异常处理器
 */
@Slf4j
@ControllerAdvice
@RestControllerAdvice
public class WpGlobalExceptionAdvice {

    @Autowired
    private AccessLogService accessLogService;

    @Autowired
    private WpProperties wpProperties;

    /**
     * 全局异常
     *
     * @param e the e
     * @return RestResponse
     */
    @ExceptionHandler(Exception.class)
    public WpResult exception(Exception e) {
        log.error("Wp Framwork Exception = {}", e.getMessage(), e);
        WpResult wpResult = new WpResult();
        wpResult.error(e.getMessage(),null);

        //　日志启用时数据库进行记录
        if(wpProperties.isLogEnable()){
            StringWriter stackTrace = new StringWriter();
            e.printStackTrace(new PrintWriter(stackTrace));

            AccessLogDto accessLogDto = (AccessLogDto) WpWebContext.getRequest().getAttribute(WpVar.LOG_DTO_KEY);
            accessLogDto.setStatus(1);
            accessLogDto.setTrace(stackTrace.toString());
            accessLogService.saveLog(accessLogDto);
        }

        return wpResult;
    }

    /**
     * 服务调用逻辑失败异常
     *
     * @param e the e
     * @return RestResponse
     */
    @ExceptionHandler(WpRestServiceException.class)
    public WpResult restServiceException(WpRestServiceException e) {
        log.error("Wp Framwork WpRestServiceException = {}", e.getMessage(), e);
        return e.getWpResult();
    }

    /**
     * 鉴权异常处理
     *
     * @param e the e
     * @return RestResponse
     */
    /*@ExceptionHandler(WpAuthenticationException.class)
    public WpResult authenticationException(Exception e) {
        log.error("Wp Framwork AuthenticationException = {}", e.getMessage(), e);
        WpResult wpResult = new WpResult();
        wpResult.failure(WpVar.DEFAULT_FAILURE_CODE,e.getMessage());
        return wpResult;
    }*/

    /**
     * 业务自定义异常处理
     *
     * @param e the e
     * @return RestResponse
     */
    /*@ExceptionHandler(WpServiceException.class)
    public WpResult serviceException(Exception e) {
        log.error("Wp Framwork ServiceException = {}", e.getMessage(), e);
        WpResult wpResult = new WpResult();
        wpResult.failure(WpVar.DEFAULT_FAILURE_CODE,e.getMessage());
        return wpResult;
    }*/

    /**
     * validation Exception
     * hibernate validate 信息
     *
     * @param exception
     * @return RestResponse
     */
    /*@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public RestResponse bodyValidExceptionHandler(MethodArgumentNotValidException exception) {
        List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
        log.warn(fieldErrors.get(0).getDefaultMessage());
        return new RestResponse().failure(fieldErrors.get(0).getDefaultMessage());
    }*/
    
    
}
